package rpc.server.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import rpc.CommonStaticSource;

public class ServiceSocket {

    private static ServerSocket serverSocket;
    private static ThreadPoolExecutor threadPoolSocket = new ThreadPoolExecutor(4, 8, 3, TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(500), new ThreadPoolExecutor.CallerRunsPolicy());
    
    static {
        try {
            serverSocket = new ServerSocket(CommonStaticSource.SOCKET_PORT);
            serverInit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private static void serverInit(){
        System.out.println("server started");
        while(true) {
            Socket socket = null;
            try {
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
                continue;
            }
            System.out.println("a client go on");
            handle(socket);
        }
    }
    
    private static void handle(Socket socket) {
        threadPoolSocket.execute(new Runnable() {
            @Override
            public void run() {
                OutputStream os = null;
                InputStream is = null;
                try {
                    os = socket.getOutputStream();
                    is = socket.getInputStream();
                } catch (IOException e2) {
                    e2.printStackTrace();
                    return;
                }
                ObjectOutputStream objectOutputStream;
                ObjectInputStream objectInputStream;
                try {
                    objectOutputStream = new ObjectOutputStream(os);
                    objectInputStream = new ObjectInputStream(is);
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
                Object obj;
                while(true){
                    try {
                        obj = objectInputStream.readObject();
                        System.out.println("server 接收到了数据");
                    } catch (ClassNotFoundException | IOException e1) {
                        e1.printStackTrace();
                        return;
                    }
                    System.out.println("client send info is :" + obj.toString());
                    try {
                        objectOutputStream.writeObject("Hello,"+obj.toString()+", I'm server");
                        System.out.println("server 已经返回结果");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    
}
